<template>
    <div>
        <!-- 搜索表单 -->
        <el-form :inline="true" :model="searchFrom" ref="searchFrom" class="demo-form-inline" style="text-align:center">
            <el-form-item label="编号" prop="id">
                <el-input v-model="searchFrom.id" placeholder="编号"></el-input>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="searchFrom.name" placeholder="名称"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="searchBtn()">查询</el-button>
                <el-button type="warning" icon="el-icon-refresh" @click="resetSearchForm()">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="id" label="编号"/>
            <el-table-column prop="name" label="配置名称"/>
            <el-table-column prop="speedBox" label="变速箱">
                <template slot-scope="scope">
                    <span v-if="scope.row.speedBox === 1"><el-button size="mini"  type="primary" round>自动挡</el-button></span>
                    <span v-if="scope.row.speedBox === 0"><el-button size="mini" type="success" round>手动挡</el-button></span>
                </template>
            </el-table-column>
            <el-table-column prop="tankCapacity" label="油箱容量(L)"/>
            <el-table-column prop="displacement" label="排量(L)"/>
            <el-table-column prop="fcphk" label="百公里油耗(L)"/>
            <el-table-column prop="seating" label="座位数(个)"/>
            <el-table-column prop="airSuction" label="进气方式"/>
            <el-table-column prop="addtime" label="添加时间"/>
            <el-table-column align="right">
                <template slot="header">
                    <span style="margin-right:30px">操作</span>
                    <el-button size="mini" type="primary" @click="handleAdd()">添加</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="block" style="text-align:center">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[1, 2, 5, 10,20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!--添加/修改弹出框-->
        <el-dialog title="详细信息" :visible.sync="dialogVisible" width="600px">
            <!--添加表单-->
            <el-form :model="configFrom" :rules="rules" ref="configFrom" class="demo-configFrom">
                <input v-model="configFrom.id" style="display: none"/>
                <el-form-item label="配置名称" label-width="120px" prop="name" style="width: 500px">
                    <el-input v-model="configFrom.name"/>
                </el-form-item>
                <el-form-item label="变速箱" label-width="120px" prop="speedBox" style="width: 500px">
                    <el-radio-group v-model="configFrom.speedBox" style="width: 500px">
                        <el-radio :label="0" border>手动挡</el-radio>
                        <el-radio :label="1" border>自动挡</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="油箱容量" label-width="120px" prop="tankCapacity" style="width: 500px">
                    <el-input-number v-model="configFrom.tankCapacity" :min="1"/>
                </el-form-item>
                <el-form-item label="排量" label-width="120px" prop="displacement" style="width: 500px">
                    <el-input-number v-model="configFrom.displacement" :min="0"/>
                </el-form-item>
                <el-form-item label="百公里油耗" label-width="120px" prop="fcphk" style="width: 500px">
                    <el-input-number v-model="configFrom.fcphk" :min="0"/>
                </el-form-item>
                <el-form-item label="座位数" label-width="120px" prop="seating" style="width: 500px">
                    <el-input-number v-model="configFrom.seating" :min="1"/>
                </el-form-item>
                <el-form-item label="进气方式" label-width="120px" prop="airSuction" style="width: 500px">
                    <el-input v-model="configFrom.airSuction"/>
                </el-form-item>

                <el-form-item label="添加时间" label-width="120px" required style="width: 500px">
                    <el-form-item prop="addtime">
                        <el-date-picker type="date" placeholder="选择日期" v-model="configFrom.addtime"/>
                    </el-form-item>
                </el-form-item>
            </el-form>
            <!--dialog的底部按钮-->
            <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm()">取 消</el-button>
                <el-button type="primary" @click="submitForm('configFrom')">确 定</el-button>
             </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        data: function () {
            // 自定义验证规则 验证添加和修改时的品牌名字是否可用
            const checkName = (rule, value, callback) => {
                this.$http.get("/cs/carConfig/checkName?name=" + value + "&id=" + this.configFrom.id)
                    .then((response) => {
                        if (response.data.resultCode === 5008) {
                            callback(new Error('该名称已存在！'));
                        } else {
                            callback();
                        }
                    })
            };
            return {
                tableData: [],
                total: 0, // 总条数
                pageNum: 1, // 当前页数
                pageSize: 5, // 每页显示几条
                searchFrom: {},
                dialogVisible: false,
                configFrom: {},// 添加和编辑的时候的通用汽车类型
                rules: {
                    name: [
                        {required: true, message: '请输入类型名称', trigger: 'blur'},
                        {trigger: 'blur', validator: checkName}//自定义验证:调方法
                    ],
                    addtime: [
                        {required: true, message: '请选择日期', trigger: 'change'}
                    ],
                    tankCapacity: [
                        {required: true, message: '请填写邮箱容量', trigger: 'blur'}
                    ],
                    displacement: [
                        {required: true, message: '请填写排量', trigger: 'blur'}
                    ],
                    fcphk: [
                        {required: true, message: '请填写百公里油耗', trigger: 'blur'}
                    ],
                    seating: [
                        {required: true, message: '请填写座位数', trigger: 'blur'}
                    ],
                    airSuction: [
                        {required: true, message: '请填写进气方式', trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            // 重置表格
            resetSearchForm() {
                this.searchFrom = {}
                this.getAllType();
            },
            // 查询表单提交
            searchBtn() {
                this.pageNum = 1;
                this.getAllType();
            },
            //每页显示几条改变
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAllType();
            },
            // 当前是第几页改变
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAllType();
            },
            // 获得表格数据
            getAllType() {
                this.$http.post('cs/carConfig/queryAll?pageSize=' + this.pageSize + '&pageNum=' + this.pageNum, this.searchFrom)
                    .then(res => {
                        this.tableData = res.data.t.list;
                        this.total = res.data.t.total
                    })
            },
            // 编辑按钮
            handleEdit(row) {
                this.configFrom = JSON.parse(JSON.stringify(row));
                this.dialogVisible = true;
            },
            // 删除按钮
            handleDelete(row) {
                this.$confirm('此操作将永久删除该类型, 是否继续?', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', center: true
                }).then(() => {
                    this.$http.delete("cs/carConfig/delete/" + row.id).then(res => {
                        if (Number(res.data.resultCode) === 2000) {
                            this.$message({type: 'success', message: '删除成功!'});
                            this.getAllType();
                        } else {
                            this.$message({type: 'error', message: '系统异常'});
                        }
                    })
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                });
            },
            // 添加按钮
            handleAdd() {
                this.configFrom = {speedBox: 1};
                this.dialogVisible = true;
            },

            // 表单提价
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.configFrom.id === undefined) {
                            // 执行添加操作
                            this.$http.post("cs/carConfig/insert", this.configFrom).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，添加成功'});
                                    this.dialogVisible = false;
                                    this.getAllType();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        } else {
                            // 执行修改操作
                            this.$http.put("cs/carConfig/update", this.configFrom).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，修改成功'});
                                    this.dialogVisible = false;
                                    this.getAllType();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        }
                    }
                });
            },

            // 重置表单
            resetForm() {
                this.configFrom = {}
                this.dialogVisible = false;
            }

        },
        mounted() {
            this.getAllType();
        }
    }
</script>

<style scoped>

</style>